http = require("http")
koa = require("koa")
co = require('co')
koaBody = require("koa-body")
Jade = require("koa-jade")

router = require("./router").getRouter()

enableDestory = require("server-destroy")
Promise = require("bluebird")

app = koa()

jade = new Jade({
    viewPath: "./jade"
})

app.use(jade.middleware)


app.use koaBody(multipart: true, formidable: {uploadDir: "/home/cda/resource", keepExtensions: true})
app.use(router.routes()).use(router.allowedMethods())


server = http.createServer(app.callback())

exports.server = server

server.on 'error', (err)->
    console.log(err)

enableDestory server

port = 8083

exports.start = ->
    yield Promise.promisify(server.listen.bind(server))(port)
    console.log("server started on " + port)

co(exports.start()).catch (err)->
    console.log(err)